/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.atjava.jcrweb.Constants;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.DirectoryInfo;
import com.atjava.jcrweb.info.UserInfo;
import com.atjava.jcrweb.manager.DirectoryManager;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.7 $ $Date: 2008/07/17 02:07:39 $
 */
public class DirectoryFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException , Exception {

		String type= this.getData(inputdata, "type");
		if(type.equals("createdir")){
			return createDir(request,response,inputdata);
		}
		if(type.equals("updatedir")){
			return updateDir(request,response,inputdata);
		}
		if(type.equals("deldir")){
			return delDir(request,response,inputdata);
		}
		return null;
	}
	private String createDir(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		DirectoryManager manager = DirectoryManager.getInstance();
		DirectoryInfo info = new DirectoryInfo();
		info.setName(getData(inputdata,"name"));
		info.setCreaterCode(getData(inputdata,"createrCode"));
		info.setCreaterName(getData(inputdata,"createrName"));
		info.setNote(getData(inputdata,"note"));
		manager.createDirectory(info, getData(inputdata,"path"));
		return null;
	}
	private String updateDir(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		DirectoryManager manager = DirectoryManager.getInstance();
		DirectoryInfo info = new DirectoryInfo();
		
		HttpSession session = request.getSession();
		if(session.getAttribute(Constants.LOGIN_USER_SESSION_KEY)!=null){
			String userCode = (String)session.getAttribute(Constants.LOGIN_USER_SESSION_KEY);
			if(Constants.ADMIN_USERNAME.equals(userCode)){
				info.setCreaterCode(Constants.ADMIN_USERNAME);
				info.setCreaterName(Constants.ADMIN_USERNAME);
			}else{
				UserInfo userInfo = (UserInfo)session.getAttribute(Constants.LOGIN_USER_SESSION_INFO);
				info.setCreaterCode(userInfo.getUserCode());
				info.setCreaterName(userInfo.getUserName());
			}
		}
		info.setId(Long.parseLong(getData(inputdata,"id")));
		info.setName(getData(inputdata,"name"));
		info.setNote(getData(inputdata,"note"));
		manager.updateDirectory(info, getData(inputdata,"path"));
		return null;
	}
	private String delDir(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception {
		DirectoryManager manager = DirectoryManager.getInstance();
		manager.deleteDirectory(getData(inputdata,"path"),Integer.valueOf(getData(inputdata,"id")) );
		return null;
	}
}
